/**
 *   Copyright (C) 2012  Development Solutions (DS) Software
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   The following additional terms will apply to the software provided with
 *   this file.
 *      1. Preserve the original authors’ name.
 *      2. Documenting all contributions and specify the contributor's name,
 *         the date of the contribution and some email to future contact.
 * 
 */
package org.dss.gil.service;

import java.util.ArrayList;
import org.apache.log4j.Logger;

/**
 *  
 *  @author  Fernando Gil <lobho.gil@gmail.com>
 *  @since   September 2012
 *  @version 1.0
 */

public class GpssService {
    
    private static Logger logger = Logger.getLogger(GpssService.class.getName());
    
    
    /*********************************************************************
     ********************* S I N G L E T O N *****************************
     *********************************************************************/
    
    private static GpssService INSTANCE = null;

    public static GpssService getInstance() {
        if (INSTANCE == null){
            INSTANCE = new GpssService();
        }
        return INSTANCE;
    }
    
    /*********************************************************************
     *********************  S E R V I C E S  *****************************
     *********************************************************************/
    public ArrayList<Object[]> parseGpssToGil(String gpss){
        ArrayList<Object[]> rows = new ArrayList<Object[]>();
        String lines[] = gpss.split("\n");
        int lineIndex = 0;
        for(String line:lines){
            if(line.trim().startsWith("*")||line.trim().isEmpty()){
            
            }else{
                logger.info(line);
                String values[] = line.split(" ");
                if(values.length==1){
                   rows.add( new Object [] {new Boolean(true), values[0],"","",""} ); 
                }else{
                    values = line.split("");
                    int index=0;
                    int lastIndex = values.length;

                    //Label
                    String label="";
                    while( index<lastIndex && index<8){
                        label+=values[index++];
                    }
                    //Block
                    String block="";
                    while(index<lastIndex && index<25){
                        block+=values[index++];
                    }
                    //Params
                    String params="";
                    while(index<lastIndex && !values[index].trim().isEmpty()){
                        params+=values[index++];
                    }
                    //Comments
                    String comments="";
                    while(index<lastIndex){
                        comments+=values[index++];
                    }
                    rows.add( new Object [] {new Boolean(true), label.trim(),block.trim(),params.trim(),comments.trim()} );
                }
            }
        }
        return rows;
    }
    
}
